﻿namespace Sustainalytics.ESGComputing.Service.Controllers
{
    using System;
    using System.Net;
    using System.Net.Http;
    using System.Web.Http;

    using Sustainalytics.ESGRatings.BusinessLogic.Computing;
    using Sustainalytics.ESGRatings.BusinessLogic.Computing.Exceptions;
    using Sustainalytics.ESGRatings.Computing.Entities.ESG;

    [RoutePrefix("api/v1/dataprovisioning/esgsets")]
    public class EsgReferenceUniverseController : ApiController
    {
        private readonly ComputationEngine _engine;

        public EsgReferenceUniverseController(ComputationEngine engine)
        {
            _engine = engine;
        }

        [Route("{setId}/referenceuniverses/{id}")]
        public HttpResponseMessage Delete(Guid setId, Guid id)
        {
            if (setId.Equals(Guid.Empty))
            {
                throw new UnknownSetException(setId);
            }

            if (id.Equals(Guid.Empty))
            {
                throw new UnknownUniverseException(id);
            }

            _engine.DeleteReferenceUniverse(setId, id);
            return Request.CreateResponse(HttpStatusCode.OK);
        }

        [Route("{setId}/referenceuniverses")]
        public HttpResponseMessage Get(Guid setId)
        {
            var data = _engine.GetUniverses(setId);

            return Request.CreateResponse(HttpStatusCode.OK, data);
        }

        [Route("{setId}/referenceuniverses")]
        public HttpResponseMessage Post(Guid setId, ReferenceUniverse data)
        {
            _engine.AddUniverses(setId, new[] { data });

            return Request.CreateResponse(HttpStatusCode.OK);
        }
    }
}